﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// Simple Service Administration Tool for WinNT based systems.
// Copyright © 2010 - Silvan Gehrig
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
// Author:
//  Silvan Gehrig
//
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Text;
using System.Collections;

#endregion

namespace SSATool.Common.Util.ExtensionMethods
{
    /// <summary>
    /// Contains the global utilities for the Exception class.
    /// </summary>
    public static class ExceptionUtil
    {
        #region Methods

        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Extracts an error string with all information of an exception.
        /// </summary>
        /// <param name="input">Input pointer from extension method.</param>
        /// <returns>Returns the generaed link.</returns>
        public static string ToFullErrorString(this Exception input)
        {
            StringBuilder errorBuilder = new StringBuilder();
            Exception current = input;
            bool first = true;

            while (current != null)
            {
                if (!first)
                    errorBuilder.AppendLine("--- inner exception ---");

                errorBuilder.Append(current.Message);
                errorBuilder.Append(" [");
                errorBuilder.Append(current.GetType().FullName);
                errorBuilder.AppendLine("]");

                if (current.StackTrace != null)
                    errorBuilder.AppendLine(current.StackTrace);

                errorBuilder.Append("HelpLink: ");
                errorBuilder.AppendLine(current.HelpLink);
                errorBuilder.Append("Source: ");
                errorBuilder.AppendLine(current.Source);
                errorBuilder.Append("Data: ");


                if (current.Data != null)
                {
                    foreach (DictionaryEntry data in current.Data)
                    {
                        errorBuilder.Append(" ");
                        errorBuilder.Append(data.Key);
                        errorBuilder.Append(" [");
                        errorBuilder.Append(data.Key.GetType().FullName);
                        errorBuilder.Append("]");

                        if (data.Value != null)
                        {
                            errorBuilder.Append(" => ");
                            errorBuilder.Append(data.Value);
                            errorBuilder.Append(" [");
                            errorBuilder.Append(data.Key.GetType().FullName);
                            errorBuilder.Append("]");
                        }
                        errorBuilder.AppendLine();
                    }
                }
                
                current = current.InnerException;
                first = false;
            }
            return errorBuilder.ToString();
        }



        #endregion
    }
}
